<?php

namespace App\Http\Controllers\Docs;

use App\Http\Requests\Docs\DocsRequests;
use App\Models\Doc;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class DocsController extends Controller
{
    /** 列表 */
    public function index(Request $request)
    {
        $model = Doc::orderBy('created_at', 'desc')->paginate(config('base.limit'));

        return view('doc.doc.index', ['model' => $model]);
    }

    /** 添加文档 */
    public function edit(Request $request)
    {

        $id    = $request->get('id');
        $model = empty($id) ? (new Doc()) : (Doc::find($id));

        return view('doc.doc.edit', ['model' => $model]);
    }

    /** 保存 */
    public function editSave(DocsRequests $requests)
    {
        $id                 = $requests->input('id');
        $data               = $requests->all();
        $model              = empty($id) ? (new Doc()) : (Doc::find($id));
        $model->name        = $data['name'];
        $model->icon        = $data['icon'];
        $model->status      = $data['status']??2;
        $model->description = $data['description'];

        if (!$model->save()) {
            return $this->fail('保存失败!');
        }

        return $this->success('保存成功!');
    }

}


